package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "菜单权限管理")
@RestController
@RequestMapping("/admin/acl/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    // 查询所有菜单权限
    @ApiOperation("查询所有菜单权限")
    @GetMapping
    public Result list() {
        List<Permission> permissionList = permissionService.queryAllPermission();
        return Result.ok(permissionList);
    }

    // 新增菜单
    @ApiOperation("新增菜单")
    @PostMapping("save")
    public Result save(@RequestBody Permission permission) {
        boolean save = permissionService.save(permission);
        return Result.ok(save);
    }

    // 修改菜单
    @ApiOperation("修改菜单")
    @PutMapping("update")
    public Result updateById(@RequestBody Permission permission) {
        boolean update = permissionService.updateById(permission);
        return Result.ok(update);
    }

    // 递归删除菜单
    @ApiOperation("递归删除菜单权限")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        permissionService.removeChildById(id);
        return Result.ok(null);
    }

    // 获取所有菜单权限，根据id获取角色已分配菜单权限列表
    @ApiOperation("获取所有菜单权限")
    @GetMapping("toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
        List<Permission> permissionList = permissionService.findRolePermissionByRoleId(roleId);
        return Result.ok(null);
    }

    // 角色分配菜单权限
    @ApiOperation("角色分配菜单权限")
    @PostMapping("doAssign")
    public Result doAssign(@RequestParam Long roleId, @RequestParam List<Long> permissionId) {
        permissionService.saveRolePermission(roleId, permissionId);
        return Result.ok(null);
    }
}
